package iosr11.gcontacts;

import java.net.URL;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.data.contacts.GroupMembershipInfo;
import com.google.gdata.data.extensions.Email;

public class ContactsImpl implements Contacts{

	private ContactsService service;
	//private static ContactsImpl contactsImpl;
	private String username;

	private static String TITLE = "iosr_team11_contacts";
	private static String PAGE = "http://www.google.com/m8/feeds/";
	private static String TYPE_C = "contacts/";
	
	public ContactsImpl(){
		service = new ContactsService(TITLE);
		username = new String();
	}
	/*
	public static ContactsImpl getInstance(){
		if (contactsImpl==null){
			contactsImpl=new ContactsImpl();
		}
		return contactsImpl;
	}*/
	
	@Override
	public ContactsService getService() {
		return service;
	}

	@Override
	public void setService(ContactsService service, String username) {
		this.service = service;
		this.username = username;
	}

	@Override
	public boolean autenticate(String username, String password){
		try{
			service.setUserCredentials(username, password);
			this.username=username;
		}
		catch (Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	@Override
	public String addNewUserReturnId(ContactEntry user) {
		try {
			URL postURL = new URL(PAGE+TYPE_C+username+"/full");
			ContactEntry afterAdd = service.insert(postURL, user);
			return afterAdd.getId();
		}
		catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String addNewUserReturnId(String realUserName, String email) {
		ContactEntry entry = new ContactEntry();
		entry.setTitle(new PlainTextConstruct(realUserName));
		Email defaultMail = new Email();
		defaultMail.setAddress(email);
		defaultMail.setRel("http://schemas.google.com/g/2005#home");
		defaultMail.setPrimary(true);
		entry.addEmailAddress(defaultMail);
		return this.addNewUserReturnId(entry);
	}
	
	@Override
	public boolean addNewUser(ContactEntry user){
		if (addNewUserReturnId(user)!=null)
			return true;
		else
			return false;
	}

	@Override
	public boolean addNewUser(String realUserName, String email) {
		if (this.addNewUserReturnId(realUserName, email) != null)
			return true;
		else
			return false;
	}

	@Override
	public ContactEntry getUser(String name) {
		try{
			URL feedURL = new URL(PAGE+TYPE_C+username+"/full");
			ContactFeed resultFeed = service.getFeed(feedURL, ContactFeed.class);
			for (ContactEntry entry : resultFeed.getEntries()){
				if (entry.getTitle().getPlainText().equals(name))
					return entry;
			}
			return null;
		}
		catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean addUserGroup(ContactEntry contact, ContactGroupEntry group) {
		GroupMembershipInfo info = new GroupMembershipInfo();
		info.setHref(group.getId());
		contact.addGroupMembershipInfo(info);
		return true;
	}

	@Override
	public boolean addUserGroup(ContactEntry contact, String groupName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ContactEntry getUser(URL url) {
		try {
			return service.getEntry(url, ContactEntry.class);
		}
		catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
}
